package com.example.myapplication.fregments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
import com.example.myapplication.interfaces.IFragmentCallback;

public class BlankFragment2 extends Fragment {

    private View root;
    private Button btn;

    private IFragmentCallback iFragmentCallback;

    public void setIFragmentCallback(IFragmentCallback iFragmentCallback) {
        this.iFragmentCallback = iFragmentCallback;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (root == null)
            root = inflater.inflate(R.layout.fragment_blank2, container, false);

        btn = root.findViewById(R.id.btn_fragment);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //向activity发送消息
                iFragmentCallback.sendMsgToActivity("你被程序猿嘲讽了，来自fragment的消息");
                //接受activity发来的消息
                String msgFromActivity = iFragmentCallback.getMsgFromActivity();
                Toast.makeText(BlankFragment2.this.getContext(), msgFromActivity, Toast.LENGTH_SHORT).show();
            }
        });

        return root;
    }
}